package com.wenlong.controller;

import com.google.gson.Gson;
import com.wenlong.pojo.Cart;
import com.wenlong.pojo.CartItem;
import com.wenlong.pojo.Commodity;
import com.wenlong.service.CommodiftyService;
import com.wenlong.service.impl.CommodittyServiceImpl;
import com.wenlong.utils.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 苏文龙
 * @version 1.0
 */
@WebServlet(name = "CartServlet", urlPatterns = {"/cartServlet"})
public class CartServlet extends BaseServlet {
    CommodiftyService cs = new CommodittyServiceImpl();

    public void addItem(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int id = WebUtils.parseInt(req.getParameter("id"), 0);
        Commodity comd = cs.queryCommodityById(id);
        CartItem cartItem = new CartItem(comd.getId(), comd.getName(), 1, comd.getPrice(), comd.getPrice());
        Cart cart = (Cart) req.getSession().getAttribute("cart");
        if (cart == null) {
            cart = new Cart();
        }
        cart.addItem(cartItem);
        req.getSession().setAttribute("cart", cart);
        req.getSession().setAttribute("lastItem",cartItem.getName());
        resp.sendRedirect(req.getHeader("Referer"));
    }

    public void AjaxaddItem(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int id = WebUtils.parseInt(req.getParameter("id"), 0);
        Commodity comd = cs.queryCommodityById(id);
        CartItem cartItem = new CartItem(comd.getId(), comd.getName(), 1, comd.getPrice(), comd.getPrice());
        Cart cart = (Cart) req.getSession().getAttribute("cart");
        if (cart == null) {
            cart = new Cart();
        }
        cart.addItem(cartItem);
        req.getSession().setAttribute("cart", cart);
        Map<String, Object> map = new HashMap<>();
        map.put("totalCart",cart.getTotalCount());
        map.put("lastItem",cartItem.getName());
        Gson gson = new Gson();
        String data = gson.toJson(map);
        resp.getWriter().write(data);
    }

    public void deleteItem(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int id = WebUtils.parseInt(req.getParameter("id"), 0);
        Cart cart = (Cart) req.getSession().getAttribute("cart");
        if (cart != null) {
            cart.deleteItem(id);
        }
        resp.sendRedirect(req.getHeader("Referer"));
    }

    public void clearItem(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Cart cart = (Cart) req.getSession().getAttribute("cart");
        if (cart != null) {
            cart.clear();
        }
        resp.sendRedirect(req.getHeader("Referer"));
    }

    public void updateCount(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int id = WebUtils.parseInt(req.getParameter("id"), 0);
        int count = WebUtils.parseInt(req.getParameter("count"), 1);
        Cart cart = (Cart) req.getSession().getAttribute("cart");
        if (cart != null) {
            cart.updateCount(id,count);
        }
        resp.sendRedirect(req.getHeader("Referer"));
    }
}
